from __future__ import annotations

from dataclasses import dataclass
from typing import List
import math


@dataclass
class RiskMetrics:
    def annualized_return(self, daily_returns: List[float], trading_days: int = 252) -> float:
        total = 1.0
        for r in daily_returns:
            total *= (1 + r)
        periods = max(len(daily_returns) / trading_days, 1e-9)
        return total ** (1 / periods) - 1

    def annualized_vol(self, daily_returns: List[float], trading_days: int = 252) -> float:
        if not daily_returns:
            return 0.0
        mean = sum(daily_returns) / len(daily_returns)
        var = sum((r - mean) ** 2 for r in daily_returns) / max(len(daily_returns) - 1, 1)
        return math.sqrt(var * trading_days)

    def sharpe(self, daily_returns: List[float], rf_daily: float = 0.0) -> float:
        excess = [r - rf_daily for r in daily_returns]
        vol = self.annualized_vol(excess)
        if vol == 0:
            return 0.0
        ar = self.annualized_return(excess)
        return ar / vol




